//
// File: Modem.cpp
//
// Purpose: Modem Control Functions
//

#include "stdafx.h"

// Default DCB setting
DCB  DefaultDCB;

BOOL InitCommConfig()
{
	// setup setting defaults
    
    DefaultDCB.DCBlength          = sizeof(DCB);
    DefaultDCB.BaudRate           = CBR_19200;
    DefaultDCB.fBinary            = TRUE;
    DefaultDCB.fParity            = TRUE;
    DefaultDCB.fOutxCtsFlow       = TRUE;
    DefaultDCB.fOutxDsrFlow       = TRUE;
    DefaultDCB.fDtrControl        = DTR_CONTROL_HANDSHAKE;
    DefaultDCB.fDsrSensitivity    = TRUE;
    DefaultDCB.fTXContinueOnXoff  = TRUE;
    DefaultDCB.fOutX              = FALSE;
    DefaultDCB.fInX               = FALSE;
    DefaultDCB.fErrorChar         = FALSE;
    DefaultDCB.fNull              = FALSE;
    DefaultDCB.fRtsControl        = RTS_CONTROL_TOGGLE;
    DefaultDCB.fAbortOnError      = TRUE;
    DefaultDCB.wReserved          = 0;
    DefaultDCB.XonLim             = 3;
    DefaultDCB.XoffLim            = 12;
    DefaultDCB.ByteSize           = 8;
    DefaultDCB.Parity             = EVENPARITY;
    DefaultDCB.StopBits           = ONESTOPBIT;
    DefaultDCB.XonChar            = 0;
    DefaultDCB.XoffChar           = 0;
    DefaultDCB.ErrorChar          = 0;
    DefaultDCB.EofChar            = 0;
    DefaultDCB.EvtChar            = 0;
    DefaultDCB.wReserved1         = 0;

#if 1
	//DefaultDCB.BaudRate           = CBR_115200;
    DefaultDCB.fParity            = TRUE;

	// CTS Flow control (check CTS ON before sending char) is Enabled
    DefaultDCB.fOutxCtsFlow       = TRUE;

	// DSR Flow control (check DSR ON before sending char) is Disabled
    DefaultDCB.fOutxDsrFlow       = FALSE;

	// Continue DTR Enable When device is Opened
    DefaultDCB.fDtrControl        = DTR_CONTROL_ENABLE;

	// Ignore DSR sensitivity (ignore chars when DSR OFF) disable
    DefaultDCB.fDsrSensitivity    = FALSE;

    DefaultDCB.fTXContinueOnXoff  = TRUE;
    DefaultDCB.fOutX              = FALSE;
    DefaultDCB.fInX               = FALSE;

    //DefaultDCB.fErrorChar         = FALSE;
    //DefaultDCB.fNull              = FALSE;

	// Enable RTS flow Control
	DefaultDCB.fRtsControl        = RTS_CONTROL_HANDSHAKE;

    //DefaultDCB.fAbortOnError      = TRUE;
    //DefaultDCB.wReserved          = 0;
    //DefaultDCB.XonLim             = 3;
    //DefaultDCB.XoffLim            = 12;
    DefaultDCB.ByteSize           = 8;
    DefaultDCB.Parity             = NOPARITY;
    DefaultDCB.StopBits           = ONESTOPBIT;
    //DefaultDCB.XonChar            = 0;
    //DefaultDCB.XoffChar           = 0;
    //DefaultDCB.ErrorChar          = 0;
    //DefaultDCB.EofChar            = 0;
    //DefaultDCB.EvtChar            = 0;
    //DefaultDCB.wReserved1         = 0;
#endif

	return TRUE;
}


BOOL SetupComm(HANDLE &hDevice)
{
    DCB          commDCB;
    COMMTIMEOUTS commTimeouts;

	//
    // Set the device configuration based on DefaultDCB
    //
    GetCommState( hDevice, &commDCB );
    commDCB.BaudRate = DefaultDCB.BaudRate;
    commDCB.ByteSize = DefaultDCB.ByteSize;
    commDCB.StopBits = DefaultDCB.StopBits;
    //commDCB.fParity = DefaultDCB.Parity;	// BUGBUG??
    commDCB.fParity = DefaultDCB.fParity;
    commDCB.Parity = DefaultDCB.Parity;

    commDCB.fOutxDsrFlow	= DefaultDCB.fOutxDsrFlow;
    commDCB.fTXContinueOnXoff	= DefaultDCB.fTXContinueOnXoff;

    commDCB.fDsrSensitivity = FALSE;
    commDCB.fDtrControl     = DTR_CONTROL_ENABLE;
    
    commDCB.fRtsControl     = DefaultDCB.fRtsControl; 
    commDCB.fOutxCtsFlow    = DefaultDCB.fOutxCtsFlow;
    commDCB.fOutX           = DefaultDCB.fOutX;
    commDCB.fInX            = DefaultDCB.fInX;

    DEBUGMSG(1,
             (TEXT("\r\nSetting port configuration :\r\n")));
    DEBUGMSG(1,
             (TEXT("Baud %d, Byte Size %d, Stop bits %d, Parity %d\r\n"),
              commDCB.BaudRate, commDCB.ByteSize, commDCB.StopBits, commDCB.Parity));
    DEBUGMSG(1,
             (TEXT("RTS Control %d, CTS Out Flow %d, XON/XOFF out/in %d/%d\r\n\r\n"),
              commDCB.fRtsControl, commDCB.fOutxCtsFlow, commDCB.fOutX, commDCB.fInX));

    SetCommState( hDevice, &commDCB );
    
    // Adjust the read/write timeouts as required by dialer thread
    GetCommTimeouts (hDevice, &commTimeouts);
#if 0
    commTimeouts.ReadIntervalTimeout = 50;
    commTimeouts.ReadTotalTimeoutMultiplier = 0;
    commTimeouts.ReadTotalTimeoutConstant = 50;
#else
    commTimeouts.ReadIntervalTimeout = 100;
    commTimeouts.ReadTotalTimeoutMultiplier = 0;
    commTimeouts.ReadTotalTimeoutConstant = 100;
#endif
    commTimeouts.WriteTotalTimeoutMultiplier = 5;
    commTimeouts.WriteTotalTimeoutConstant = 500;
    SetCommTimeouts (hDevice, &commTimeouts);

	// NO CHECK at this point
	return TRUE;
}
